package nc.impl.hrpbm.budgetreply.budgetreply;

import nc.bs.framework.common.NCLocator;
import nc.impl.pubapp.pattern.data.bill.BillQuery;
import nc.itf.hrpbm.fourcgxm.fourcgxm.IFourcgxmVOService;
import nc.itf.hrpbm.sjxm.sjxmh.ISjxmHVOService;
import nc.vo.fourcgxm.fourcgxm.AggFourcgxmVO;
import nc.vo.fourcgxm.fourcgxm.FourcgxmVO;
import nc.vo.hrpbm.budgetswap.AggBudgetswapVO;
import nc.vo.hrpbm.budgetswap.BudgetswapBVO;
import nc.vo.hrpbm.budgetswap.BudgetswapVO;
import nc.vo.hrppl.pjapplication.AggPjapplVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.ISuperVO;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFDouble;
import nc.vo.hrppl.pjapplication.PjapplPVO;
import nc.vo.sjxm.sjxm.AggSjxmVO;
import nc.vo.sjxm.sjxm.SjxmBVO;
import nc.vo.sjxm.sjxm.SjxmHVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class BudgetSwapWriteBackForPeojectUtil {
    private ISjxmHVOService sjxmHVOService;
    BillQuery<AggPjapplVO> querySQD = new BillQuery<AggPjapplVO>(AggPjapplVO.class);
    BillQuery<AggSjxmVO> queryCG = new BillQuery<AggSjxmVO>(AggSjxmVO.class);

    /**
     * 预算调剂-审批通过回写四级采购项目
     * @param vos
     */
    public static void ApproveWriteBackForProjectUtil(AggBudgetswapVO[] vos) throws BusinessException {
        BudgetSwapWriteBackForPeojectUtil util = new BudgetSwapWriteBackForPeojectUtil();
        if (vos == null) return;
        for (AggBudgetswapVO vo : vos) {
            if (vo == null) continue;
            BudgetswapVO hvo = vo.getParentVO();
            String nyear = hvo.getNyear();
            BudgetswapBVO[] bvos = (BudgetswapBVO[])vo.getChildren(BudgetswapBVO.class);
            if (bvos == null || bvos.length == 0) continue;
            List<AggSjxmVO> uapaggvos = new ArrayList<>();
            for (BudgetswapBVO bvo : bvos) {
                if (bvo == null) continue;
                String pk_project_out = bvo.getAttributeValue("pk_project_out").toString(); // 转出项目
                UFDouble amount_out = bvo.getAmount_out();// 转出前的预算金额
                String pk_project_in = bvo.getAttributeValue("pk_project_in").toString(); // 转入项目
                UFDouble amount_in = bvo.getAmount_in(); // 转入前的预算金额
                UFDouble swap_amount = bvo.getSwap_amount(); // 调剂金额
                UFDouble zczzMonery = amount_out.sub(swap_amount); // 转出项目最终金额
                UFDouble zrzzMonery = amount_in.add(swap_amount); // 转入后最终金额
                AggPjapplVO[] zcaggvos = util.querySQD.query(new String[]{pk_project_out});// 转出
                List<AggSjxmVO>  zcvos = util.getAggFourcgxmVOList(zcaggvos, swap_amount, zczzMonery , "zc", nyear);
                AggPjapplVO[] zraggvos = util.querySQD.query(new String[]{pk_project_in}); // 转入
                List<AggSjxmVO> zrvos = util.getAggFourcgxmVOList(zraggvos, swap_amount, zrzzMonery, "zr", nyear);
                if(zcvos != null && zcvos.size() > 0){
                    uapaggvos.addAll(zcvos);
                }
                if(zrvos != null && zrvos.size() > 0){
                    uapaggvos.addAll(zrvos);
                }

            }
            if(uapaggvos != null && !uapaggvos.isEmpty()){
                util.getSjxmHVOService().saveAggSjxmVO(uapaggvos.toArray(new AggSjxmVO[0]));
            }
        }
    }

    /**
     *
     * 通过项目申请单获取，采购项目
     * @param zcaggvos
     * @param tzMonery 调整金额
     * @param zzMonery 最终金额
     * @param type 操作方式  zc:转出、 zr:转入
     * @param nyear 年度
     * @return
     * @throws BusinessException
     */
    public List<AggSjxmVO> getAggFourcgxmVOList(AggPjapplVO[] zcaggvos, UFDouble tzMonery, UFDouble zzMonery, String type, String nyear) throws BusinessException{
        BudgetSwapWriteBackForPeojectUtil util = new BudgetSwapWriteBackForPeojectUtil();
        List<AggSjxmVO> lists = new ArrayList<>();
        if (zcaggvos == null || zcaggvos.length == 0) return null;
        List<String> pks = new ArrayList<>(); // 转出的四级项目
        for (AggPjapplVO vo : zcaggvos ){
            if (vo == null) continue;
            PjapplPVO[] cgvos = (PjapplPVO[])vo.getChildren(PjapplPVO.class);
            if (cgvos == null) continue;
            for (PjapplPVO cgvo : cgvos){
                String vbdef1 = cgvo.getVbdef1(); // 采购项目
                if ( vbdef1 != null ){
                    pks.add(vbdef1);
                }
            }
        }
        String msg = "转出";
        String msg1 = "转入";
        if("zr".equals(type)){
            msg = "转入";
            msg1 = "转入";
        }
        if(pks.isEmpty()){
            return null;
        }
        AggSjxmVO[] cgaggvos = util.getSjxmHVOService().listAggSjxmVOByPk(pks.toArray(new String[0]));
        if (cgaggvos == null) return lists;
        for (AggSjxmVO cgvo : cgaggvos){
            SjxmHVO hvo = cgvo.getParentVO();
            if("zc".equals(type)){
                tzMonery = tzMonery.multiply(-1); // 转出减少
            }else{
                tzMonery = tzMonery.multiply(1); // 增加
            }
            SjxmBVO[] bvos = (SjxmBVO[])cgvo.getChildren(SjxmBVO.class);
            if (Objects.isNull(bvos)) continue;
            Boolean flag = false;
            for (SjxmBVO bvo : bvos) {
                if (nyear.equals(bvo.getYsyear())){ // 存在转出的年度预算
                    bvo.setStatus(VOStatus.UPDATED);
                    UFDouble threeprotzmoney = bvo.getThreeprotzmoney();
                    bvo.setAttributeValue("kymoney", zzMonery.toString()); // 可用金额
                    bvo.setAttributeValue("threeprofinysmoney", zzMonery); // 三级项目最终预算金额
                    bvo.setAttributeValue("threeproysmoney", zzMonery); // 三级项目预算金额
                    UFDouble tz = tzMonery.add(threeprotzmoney); // 最终调整金额为 中途调整金额加调剂的金额
                    bvo.setAttributeValue("threeprotzmoney",tz); // 调整金额
                    flag  = true;
                    break;
                }
            }
            if(flag){
                hvo.setStatus(VOStatus.UPDATED);
                lists.add(cgvo);
            }
        }
        return lists;
    }

    /**
     * 预算调剂-取消审批会写四级采购项目
     * @param vos
     */
    public static void UnApproveWriteBackForProjectUtil(AggBudgetswapVO[] vos) throws BusinessException{
        BudgetSwapWriteBackForPeojectUtil util = new BudgetSwapWriteBackForPeojectUtil();
        if (vos == null) return;
        for (AggBudgetswapVO vo : vos) {
            if (vo == null) continue;

        }
    }

    public ISjxmHVOService getSjxmHVOService() {
        if(null == this.sjxmHVOService){
            this.sjxmHVOService = NCLocator.getInstance().lookup(ISjxmHVOService.class);
        }
        return sjxmHVOService;
    }
}
